<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>fastNLP - 轻量级自然语言处理工具包</title>
    <style>
        /* 基础样式 */
        :root {
            --primary-color: #4c6ef5;
            --secondary-color: #339af0;
            --accent-color: #3bc9db;
            --text-color: #212529;
            --light-text: #6c757d;
            --bg-color: #f8f9fa;
            --card-bg: #ffffff;
            --border-color: #dee2e6;
            --code-bg: #f1f3f5;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
            line-height: 1.6;
            color: var(--text-color);
            background-color: var(--bg-color);
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
        }

        /* 头部样式 */
        header {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            padding: 80px 0 60px;
            text-align: center;
        }

        .header-content {
            max-width: 800px;
            margin: 0 auto;
        }

        h1 {
            font-size: 3rem;
            margin-bottom: 1rem;
            font-weight: 700;
        }

        .subtitle {
            font-size: 1.25rem;
            margin-bottom: 2rem;
            opacity: 0.9;
        }

        /* 导航样式 */
        nav {
            background-color: var(--card-bg);
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .nav-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 1rem 20px;
        }

        .logo {
            font-weight: 700;
            color: var(--primary-color);
            text-decoration: none;
            font-size: 1.25rem;
        }

        .nav-links {
            display: flex;
            gap: 1.5rem;
        }

        .nav-links a {
            color: var(--text-color);
            text-decoration: none;
            font-weight: 500;
            transition: color 0.3s;
        }

        .nav-links a:hover {
            color: var(--primary-color);
        }

        /* 主要内容样式 */
        main {
            padding: 40px 0;
        }

        section {
            margin-bottom: 60px;
        }

        h2 {
            font-size: 2rem;
            margin-bottom: 1.5rem;
            color: var(--primary-color);
            position: relative;
            padding-bottom: 10px;
        }

        h2::after {
            content: '';
            position: absolute;
            left: 0;
            bottom: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, var(--primary-color), var(--accent-color));
            border-radius: 2px;
        }

        h3 {
            font-size: 1.5rem;
            margin: 1.5rem 0 1rem;
            color: var(--secondary-color);
        }

        p {
            margin-bottom: 1rem;
        }

        /* 卡片样式 */
        .card {
            background-color: var(--card-bg);
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
            padding: 2rem;
            margin-bottom: 1.5rem;
            transition: transform 0.3s, box-shadow 0.3s;
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
        }

        /* 特性卡片 */
        .features {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 2rem;
            margin-top: 2rem;
        }

        .feature-card {
            background-color: var(--card-bg);
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
            padding: 2rem;
            transition: transform 0.3s, box-shadow 0.3s;
        }

        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
        }

        .feature-icon {
            font-size: 2.5rem;
            color: var(--primary-color);
            margin-bottom: 1rem;
        }

        /* 代码块样式 */
        pre {
            background-color: var(--code-bg);
            border-radius: 6px;
            padding: 1.5rem;
            overflow-x: auto;
            margin: 1.5rem 0;
            font-family: 'Fira Code', 'Consolas', monospace;
            font-size: 0.9rem;
        }

        code {
            font-family: 'Fira Code', 'Consolas', monospace;
            background-color: var(--code-bg);
            padding: 0.2rem 0.4rem;
            border-radius: 4px;
            font-size: 0.9rem;
        }

        /* 表格样式 */
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 1.5rem 0;
            background-color: var(--card-bg);
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        }

        th, td {
            padding: 1rem;
            text-align: left;
            border-bottom: 1px solid var(--border-color);
        }

        th {
            background-color: rgba(76, 110, 245, 0.1);
            font-weight: 600;
            color: var(--primary-color);
        }

        tr:last-child td {
            border-bottom: none;
        }

        /* 列表样式 */
        ul, ol {
            margin: 1rem 0 1rem 1.5rem;
        }

        li {
            margin-bottom: 0.5rem;
        }

        /* 按钮样式 */
        .btn {
            display: inline-block;
            background-color: var(--primary-color);
            color: white;
            padding: 0.75rem 1.5rem;
            border-radius: 6px;
            text-decoration: none;
            font-weight: 500;
            transition: background-color 0.3s, transform 0.3s;
            border: none;
            cursor: pointer;
            font-size: 1rem;
        }

        .btn:hover {
            background-color: #3b5bdb;
            transform: translateY(-2px);
        }

        .btn-outline {
            background-color: transparent;
            color: var(--primary-color);
            border: 2px solid var(--primary-color);
        }

        .btn-outline:hover {
            background-color: var(--primary-color);
            color: white;
        }

        /* 安装区域 */
        .installation {
            background-color: rgba(76, 110, 245, 0.05);
            padding: 2rem;
            border-radius: 8px;
            margin: 2rem 0;
        }

        /* 折叠面板 */
        .collapsible {
            background-color: var(--card-bg);
            border-radius: 8px;
            overflow: hidden;
            margin-bottom: 1rem;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        }

        .collapsible-header {
            padding: 1rem 1.5rem;
            background-color: rgba(76, 110, 245, 0.1);
            cursor: pointer;
            font-weight: 600;
            display: flex;
            justify-content: space-between;
            align-items: center;
            transition: background-color 0.3s;
        }

        .collapsible-header:hover {
            background-color: rgba(76, 110, 245, 0.15);
        }

        .collapsible-content {
            padding: 0;
            max-height: 0;
            overflow: hidden;
            transition: max-height 0.3s, padding 0.3s;
        }

        .collapsible-content-inner {
            padding: 1.5rem;
        }

        .collapsible.active .collapsible-content {
            max-height: 2000px;
            padding: 0;
        }

        /* 页脚样式 */
        footer {
            background-color: var(--primary-color);
            color: white;
            padding: 2rem 0;
            text-align: center;
        }

        .footer-content {
            max-width: 800px;
            margin: 0 auto;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            h1 {
                font-size: 2.25rem;
            }

            .nav-container {
                flex-direction: column;
                gap: 1rem;
            }

            .nav-links {
                flex-wrap: wrap;
                justify-content: center;
                gap: 1rem;
            }

            .features {
                grid-template-columns: 1fr;
            }

            .card {
                padding: 1.5rem;
            }
        }
    </style>
</head>
<body>
    <!-- 头部区域 -->
    <header>
        <div class="container header-content">
            <h1>fastNLP</h1>
            <p class="subtitle">轻量级自然语言处理工具包，让NLP开发更简单、高效</p>
        </div>
    </header>

    <!-- 导航栏 -->
    <nav>
        <div class="container nav-container">
            <a href="#" class="logo">fastNLP</a>
            <div class="nav-links">
                <a href="#overview">项目概述</a>
                <a href="#features">主要特性</a>
                <a href="#structure">项目结构</a>
                <a href="#usage">使用方法</a>
                <a href="#advanced">进阶功能</a>
            </div>
        </div>
    </nav>

    <!-- 主要内容 -->
    <main class="container">
        <!-- 项目概述 -->
        <section id="overview">
            <h2>项目概述</h2>
            <div class="card">
                <p>fastNLP是一款轻量级的自然语言处理（NLP）工具包，旨在减少用户项目中的工程型代码，如数据处理循环、训练循环、多卡运行等。通过提供简洁易用的API，fastNLP让研究人员和开发者能够专注于模型设计和实验，而不是繁琐的工程细节。</p>
                
                <div class="installation">
                    <h3>快速安装</h3>
                    <pre><code>pip install fastNLP>=1.0.0alpha</code></pre>
                    <p>同时需要安装相应的深度学习框架：</p>
                    <ul>
                        <li>PyTorch: <code>pip install torch>=1.6.0</code></li>
                        <li>Paddle: 安装对应版本的paddlepaddle</li>
                        <li>Jittor/OneFlow: 安装对应框架</li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- 主要特性 -->
        <section id="features">
            <h2>主要特性</h2>
            <div class="features">
                <div class="feature-card">
                    <div class="feature-icon">⚡</div>
                    <h3>便捷</h3>
                    <p>提供apply函数避免循环、支持多进程提速、训练循环阶段可方便定制操作。通过简洁的API设计，大大减少代码量。</p>
                </div>
                <div class="feature-card">
                    <div class="feature-icon">🚀</div>
                    <h3>高效</h3>
                    <p>无需改动代码，实现fp16切换、多卡训练、ZeRO优化等。优化的数据加载和处理流程，提升训练效率。</p>
                </div>
                <div class="feature-card">
                    <div class="feature-icon">🔄</div>
                    <h3>兼容</h3>
                    <p>支持PyTorch、Paddle、Jittor、OneFlow等多种深度学习框架作为后端，实现代码一次编写，多框架运行。</p>
                </div>
            </div>
        </section>

        <!-- 项目结构 -->
        <section id="structure">
            <h2>项目结构</h2>
            
            <h3>根目录文件</h3>
            <table>
                <thead>
                    <tr>
                        <th>文件/目录</th>
                        <th>作用</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><code>.Jenkinsfile</code></td>
                        <td>CI/CD配置文件，用于持续集成</td>
                    </tr>
                    <tr>
                        <td><code>.github/</code></td>
                        <td>GitHub配置目录，包含issue模板、PR模板等</td>
                    </tr>
                    <tr>
                        <td><code>LICENSE</code></td>
                        <td>开源许可证文件</td>
                    </tr>
                    <tr>
                        <td><code>README.md</code></td>
                        <td>项目说明文档</td>
                    </tr>
                    <tr>
                        <td><code>docs/</code></td>
                        <td>详细文档目录</td>
                    </tr>
                    <tr>
                        <td><code>fastNLP/</code></td>
                        <td>主源码目录</td>
                    </tr>
                    <tr>
                        <td><code>requirements.txt</code></td>
                        <td>项目依赖列表</td>
                    </tr>
                    <tr>
                        <td><code>setup.py</code></td>
                        <td>安装配置文件</td>
                    </tr>
                    <tr>
                        <td><code>tests/</code></td>
                        <td>测试代码目录</td>
                    </tr>
                    <tr>
                        <td><code>tutorials/</code></td>
                        <td>教程和示例代码</td>
                    </tr>
                </tbody>
            </table>

            <h3>核心模块</h3>
            
            <div class="collapsible">
                <div class="collapsible-header">
                    <span>core/ - 核心功能模块</span>
                    <span>+</span>
                </div>
                <div class="collapsible-content">
                    <div class="collapsible-content-inner">
                        <p>core模块实现了fastNLP的核心功能，主要包含以下子模块：</p>
                        <table>
                            <thead>
                                <tr>
                                    <th>子模块</th>
                                    <th>主要功能</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td><code>callbacks/</code></td>
                                    <td>回调函数，用于训练过程中的各种事件处理（进度显示、学习率调整、早停等）</td>
                                </tr>
                                <tr>
                                    <td><code>collators/</code></td>
                                    <td>数据批处理工具，负责将数据样本组装成批次</td>
                                </tr>
                                <tr>
                                    <td><code>controllers/</code></td>
                                    <td>控制训练和评估流程的组件，包括Trainer、Evaluator等</td>
                                </tr>
                                <tr>
                                    <td><code>dataloaders/</code></td>
                                    <td>数据加载器，支持多种框架（PyTorch、Paddle、Jittor、OneFlow）</td>
                                </tr>
                                <tr>
                                    <td><code>dataset/</code></td>
                                    <td>数据集相关类，如DataSet、FieldArray、Instance等</td>
                                </tr>
                                <tr>
                                    <td><code>drivers/</code></td>
                                    <td>驱动模块，实现不同框架间的兼容</td>
                                </tr>
                                <tr>
                                    <td><code>log/</code></td>
                                    <td>日志工具</td>
                                </tr>
                                <tr>
                                    <td><code>metrics/</code></td>
                                    <td>评估指标，如Accuracy、F1分数等</td>
                                </tr>
                                <tr>
                                    <td><code>samplers/</code></td>
                                    <td>采样器，用于数据集采样策略</td>
                                </tr>
                                <tr>
                                    <td><code>utils/</code></td>
                                    <td>工具函数集合</td>
                                </tr>
                                <tr>
                                    <td><code>vocabulary.py</code></td>
                                    <td>词汇表实现</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="collapsible">
                <div class="collapsible-header">
                    <span>io/ - 数据输入输出模块</span>
                    <span>+</span>
                </div>
                <div class="collapsible-content">
                    <div class="collapsible-content-inner">
                        <p>io模块提供数据读取、处理和保存功能：</p>
                        <table>
                            <thead>
                                <tr>
                                    <th>子模块</th>
                                    <th>主要功能</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td><code>data_bundle.py</code></td>
                                    <td>数据集管理类DataBundle</td>
                                </tr>
                                <tr>
                                    <td><code>embed_loader.py</code></td>
                                    <td>词嵌入加载器</td>
                                </tr>
                                <tr>
                                    <td><code>file_reader.py</code></td>
                                    <td>文件读取工具</td>
                                </tr>
                                <tr>
                                    <td><code>file_utils.py</code></td>
                                    <td>文件操作工具</td>
                                </tr>
                                <tr>
                                    <td><code>loader/</code></td>
                                    <td>各种格式数据加载器（分类、CoNLL、CSV、JSON、匹配、问答等）</td>
                                </tr>
                                <tr>
                                    <td><code>pipe/</code></td>
                                    <td>数据处理管道，用于数据预处理和转换</td>
                                </tr>
                                <tr>
                                    <td><code>utils.py</code></td>
                                    <td>工具函数</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="collapsible">
                <div class="collapsible-header">
                    <span>其他核心模块</span>
                    <span>+</span>
                </div>
                <div class="collapsible-content">
                    <div class="collapsible-content-inner">
                        <table>
                            <thead>
                                <tr>
                                    <th>模块</th>
                                    <th>主要功能</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td><code>embeddings/</code></td>
                                    <td>提供将序列索引转换为向量序列的功能，包括读取预训练词嵌入等</td>
                                </tr>
                                <tr>
                                    <td><code>models/</code></td>
                                    <td>实现了一些完整的神经网络模型，如文本分类模型（CNN、LSTM等）、序列标注模型（BiLSTM+CRF等）</td>
                                </tr>
                                <tr>
                                    <td><code>modules/</code></td>
                                    <td>实现了用于搭建神经网络模型的基础组件：LSTM、GRU等循环神经网络组件、Transformer相关组件、CRF条件随机场、框架转换工具等</td>
                                </tr>
                                <tr>
                                    <td><code>envs/</code></td>
                                    <td>管理fastNLP的运行环境：框架后端设置、分布式训练配置、环境变量管理</td>
                                </tr>
                                <tr>
                                    <td><code>transformers/</code></td>
                                    <td>提供对Transformer系列预训练模型的支持</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </section>

        <!-- 使用方法 -->
        <section id="usage">
            <h2>使用方法</h2>
            
            <h3>基本使用流程</h3>
            
            <div class="card">
                <h4>1. 数据加载与处理</h4>
                <pre><code>from fastNLP.io import DataBundle, SST2Loader, SST2Pipe

# 加载数据
loader = SST2Loader()
data_bundle = loader.load('path/to/data')

# 预处理数据
pipe = SST2Pipe()
data_bundle = pipe.process(data_bundle)

# 数据分割
train_data = data_bundle.get_dataset('train')
dev_data = data_bundle.get_dataset('dev')
test_data = data_bundle.get_dataset('test')</code></pre>
            </div>
            
            <div class="card">
                <h4>2. 定义模型</h4>
                <pre><code>import torch
import torch.nn as nn
from fastNLP.modules import LSTM
from fastNLP.models import CNNText

# 使用预定义模型
model = CNNText((len(vocab), 300), num_classes=2)

# 或者自定义模型
class CustomModel(nn.Module):
    def __init__(self, vocab_size, embedding_dim, hidden_dim, num_classes):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, embedding_dim)
        self.lstm = LSTM(embedding_dim, hidden_dim, bidirectional=True)
        self.fc = nn.Linear(hidden_dim * 2, num_classes)
    
    def forward(self, words):
        x = self.embedding(words)
        x = self.lstm(x)[0]
        x = torch.max(x, dim=1)[0]
        x = self.fc(x)
        return {'pred': x}</code></pre>
            </div>
            
            <div class="card">
                <h4>3. 训练与评估</h4>
                <pre><code>from fastNLP import Trainer, Evaluator, Accuracy
from fastNLP import TorchDataLoader

# 准备数据加载器
train_loader = TorchDataLoader(train_data, batch_size=32, shuffle=True)
dev_loader = TorchDataLoader(dev_data, batch_size=64)

# 定义优化器和损失函数
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()

# 训练模型
trainer = Trainer(
    model=model,
    train_dataloader=train_loader,
    optimizers=optimizer,
    loss=criterion,
    device=0,  # 使用第0块GPU
    callbacks=[ProgressCallback()]
)
trainer.train()

# 评估模型
evaluator = Evaluator(
    model=model,
    dataloaders=dev_loader,
    metrics={'acc': Accuracy()}
)
eval_results = evaluator.run()
print(eval_results)</code></pre>
            </div>
        </section>

        <!-- 进阶功能 -->
        <section id="advanced">
            <h2>进阶功能</h2>
            
            <div class="card">
                <h3>多卡训练</h3>
                <pre><code># 简单设置device为显卡列表即可实现多卡训练
trainer = Trainer(
    model=model,
    train_dataloader=train_loader,
    optimizers=optimizer,
    loss=criterion,
    device=[0, 1],  # 使用0号和1号GPU
    callbacks=[ProgressCallback()]
)
trainer.train()</code></pre>
            </div>
            
            <div class="card">
                <h3>不同框架切换</h3>
                <pre><code># PyTorch
trainer = Trainer(
    model=torch_model,
    train_dataloader=TorchDataLoader(train_data),
    optimizers=torch.optim.Adam(torch_model.parameters()),
    driver='torch'  # 或自动识别
)

# Paddle
trainer = Trainer(
    model=paddle_model,
    train_dataloader=PaddleDataLoader(train_data),
    optimizers=paddle.optimizer.Adam(parameters=paddle_model.parameters()),
    driver='paddle'  # 或自动识别
)</code></pre>
            </div>
            
            <div class="card">
                <h3>自定义Callback</h3>
                <pre><code>from fastNLP import Callback, Event

class MyCallback(Callback):
    def on_epoch_begin(self):
        print(f"Epoch {self.epoch_idx} starts")
    
    def on_batch_end(self):
        if self.batch_idx % 100 == 0:
            print(f"Batch {self.batch_idx}, Loss: {self.last_loss}")

trainer = Trainer(
    # ...其他参数
    callbacks=[ProgressCallback(), MyCallback()]
)</code></pre>
            </div>
            
            <div class="card">
                <h3>使用预训练词向量</h3>
                <pre><code>from fastNLP.embeddings import StaticEmbedding

# 加载预训练词向量
embed = StaticEmbedding(vocab, model_dir_or_name='glove.6B.300d')

# 在模型中使用
model = nn.Sequential(
    embed,
    # ...其他层
)</code></pre>
            </div>
            
            <div class="card">
                <h3>使用Transformer预训练模型</h3>
                <pre><code>from fastNLP.transformers import BertModel

# 加载BERT模型
bert = BertModel.from_pretrained('bert-base-uncased')

# 用于下游任务
class BertClassifier(nn.Module):
    def __init__(self):
        super().__init__()
        self.bert = BertModel.from_pretrained('bert-base-uncased')
        self.dropout = nn.Dropout(0.1)
        self.classifier = nn.Linear(768, 2)
    
    def forward(self, input_ids, attention_mask):
        outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask)
        pooled_output = outputs.pooler_output
        pooled_output = self.dropout(pooled_output)
        logits = self.classifier(pooled_output)
        return {'pred': logits}</code></pre>
            </div>
        </section>
    </main>

    <!-- 页脚 -->
    <footer>
        <div class="container footer-content">
            <p>fastNLP - 轻量级自然语言处理工具包</p>
            <p>&copy; fastNLP团队</p>
        </div>
    </footer>

    <script>
        // 折叠面板功能
        document.addEventListener('DOMContentLoaded', function() {
            const collapsibles = document.querySelectorAll('.collapsible');
            
            collapsibles.forEach(item => {
                const header = item.querySelector('.collapsible-header');
                
                header.addEventListener('click', function() {
                    item.classList.toggle('active');
                    const sign = header.querySelector('span:last-child');
                    sign.textContent = item.classList.contains('active') ? '-' : '+';
                });
            });
            
            // 默认展开第一个折叠面板
            if (collapsibles.length > 0) {
                collapsibles[0].classList.add('active');
                const sign = collapsibles[0].querySelector('.collapsible-header span:last-child');
                sign.textContent = '-';
            }
        });
    </script>
</body>
</html>